package com.park.controller;

import com.park.domain.User;
import com.park.repository.DatabaseUserRepository;
import com.park.service.UserService;
import com.park.service.impl.UserServiceImpl;
import com.park.sql.DbConnectionManager;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Path;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 注册功能实现
 *
 * @author Aaron
 * @date 2021/3/1 22:06
 */
@Path("/register")
public class RegisterController implements Controller {
    private static final Logger logger = Logger.getLogger(RegisterController.class.getName());


    @Path("/register")
    public String register(HttpServletRequest request, HttpServletResponse response) {
        logger.info("进入 register controller ");
        logger.info("name: " + request.getParameter("name"));
        logger.info("password: " + request.getParameter("password"));
        logger.info("email: " + request.getParameter("email"));
        logger.info("mobile: " + request.getParameter("mobile"));
        UserService userService = new UserServiceImpl(new DatabaseUserRepository(new DbConnectionManager()));
        User user = new User();
        user.setName(request.getParameter("name"));
        user.setPassword(request.getParameter("password"));
        user.setEmail(request.getParameter("email"));
        user.setPhoneNumber(request.getParameter("mobile"));
        userService.register(user);
        logger.log(Level.INFO, "all:" + userService.getAll());
        request.setAttribute("userList", userService.getAll());
        return "register.jsp";
    }
}
